package fr.inserm.umr915.vcfserver.bio.oneKgenomes;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import com.sleepycat.bind.tuple.TupleBinding;


import fr.inserm.umr915.vcfserver.bio.Chromosomes;
import fr.inserm.umr915.vcfserver.bio.Segment;
import fr.inserm.umr915.vcfserver.bio.berkeley.DatabaseManager;
import fr.inserm.umr915.vcfserver.util.ColumnSerializer;
import fr.inserm.umr915.vcfserver.util.NameExtractor;
import fr.inserm.umr915.vcfserver.xml.XmlStreamSerializer;



public class Snp1000GHandler extends DatabaseManager<Snp1000G>
	{
	private Snp1000G.Binding dataBinding;
	private Snp1000G.StaxSerializer stax=new Snp1000G.StaxSerializer();
	private Snp1000G.DelimitedOutputSerializer columnSerializer=new Snp1000G.DelimitedOutputSerializer();
	
	
	private NameExtractor<Snp1000G> nameExtactor=new NameExtractor<Snp1000G>()
		{
		@Override
		public String getName(Snp1000G o)
			{
			return o==null?null:o.getId()==null?o.getChrom()+":"+o.getPosition0():o.getId();
			}
		};
	
	public Snp1000GHandler(Chromosomes chromosomes)
		{
		dataBinding=new Snp1000G.Binding(chromosomes);
		}
	
	@Override
	public String getListName() {
		return "Snp1000GList";
		}
	
	@Override
	public NameExtractor<Snp1000G> getNameExtractor() {
		return nameExtactor;
		}
	
	@Override
	public ColumnSerializer<Snp1000G> getColumnSerializer() {
		return columnSerializer;
		}
	
	@Override
	public TupleBinding< Snp1000G> getDataBinding()
		{
		return this.dataBinding;
		}
	
	@Override
	public XmlStreamSerializer<Snp1000G> getXmlStreamSerializer()
		{
		return stax;
		}
	
	@Override
	public Snp1000G parse(String line) throws IOException
		{
		if(line.startsWith("#")) return null;
		Snp1000G g= Snp1000G.parseVCF(getDelim().split(line));
		if(!acceptChromosome(g.getChrom())) return null;
		return g;
		}
	@Override
	protected Segment getSegment(Snp1000G o) {
		return new Segment(o.getChrom(),o.getPosition0(),o.getPosition0()+1);
		}
	@Override
	protected Set<String> getNames(Snp1000G g)
		{
		Set<String> set=new HashSet<String>();
		if(g.getId()!=null)
			{
			for(String s: g.getId().split("[;]"))
				{
				if(s.isEmpty()) continue;
				set.add(s);
				}
			}
		return set;
		}
	
	
	}
